<? session_start();
						
include_once ('core/conn.php');
						
$db = get_conn();
						
check_session();
					
						
					
?>
<html>
<head>
<link href="tools/modal/base.css" type="text/css" rel="stylesheet" />
<link href="tools/modal/modal-window.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" lang="javascript" src="tools/modal/jquery-1.3.2.min.js"></script>
<script type="text/javascript" lang="javascript" src="tools/modal/modal-window.min.js"></script>
<script>
function refresh_list(){
	document.location.href=document.location.href;	

}

function CheckAll(){
	
	/*
	var x = document.getElementById('finish');
	x.disabled = false;
*/
	var x = document.getElementById('delete_selected');
	x.disabled = false;


	
	for(i=0;i<document.getElementsByTagName("input").length;i++){
		if(document.getElementsByTagName("input")[i].type == "checkbox"){
			document.getElementsByTagName("input")[i].checked =true;				
		}
	}
}

function UnCheckAll(chk){
	for(i=0;i<document.getElementsByTagName("input").length;i++){
		if(document.getElementsByTagName("input")[i].type == "checkbox"){
			document.getElementsByTagName("input")[i].checked =false;				
		}
	}
}	


	function do_checker(){

		var text = document.getElementById("xxx").value;
		if(text =='Seleccionar todos'){
			document.getElementById("xxx").value ='Desmarcar todos';
			CheckAll();	
		}else{
			document.getElementById("xxx").value ='Seleccionar todos';
			UnCheckAll();				
		}
	}
	
	
	function checkdata(obj){
		if(confirm("Esta seguro?")){
			return true;	
		}
		return false;
	}
</script>
</head>

<body>
<form name='invoices' method='post' action='/core/invoices.php' onsubmit='return checkdata(this)'>
  <table width="100%" border="0">
    <tr>
      <td>Cobros Pendientes      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><input type="button" name="button" id="xxx" onClick="do_checker()" value="Seleccionar todos">
        <span style="text-align:center">
        <input type="submit" name="delete_selected" id="delete_selected" <?php if($btn_disabled){echo " disabled ";} ?> value="Eliminar" disabled>
      </span></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td><? 
			
		if($_SESSION['current_location']!=''){
			$owner_filter = "i.id_location = ".$_SESSION['current_location']." and ";	
		}
		$sql = "select i.id_invoice, i.amount, i.due_date, i.exp_date, i.description, l.number, l.id_location, it.name
				from 
				`invoices` i , 
				`locations` l ,
				`invoices_type` it
				where 
				i.istatus='P' and $owner_filter
				i.id_location = l.id_location and
				i.id_it = it.id_it
				order by l.number";
			
		$result = mysql_query($sql);
			
		if($result){
				
			$data ="<table width='100%' border='0' align='left' >";
				
			if(mysql_num_rows($result) > 0){
					$data .="<tr>

						<td align='left' >
							ID
						</td>
						<td align='left' >
							Filial
						</td>							
						<td align='left' >
							Tipo
						</td>
						
						<td align='left' >
							Fecha Cobro
						</td>						
						<td align='left' >
							Monto
						</td>	
						<td align='left' >
							Opciones
						</td>																								
						</tr>";					
				while($row = mysql_fetch_array($result)){
					$data .="<tr>
						<td align='left' >
						<input type='checkbox' name='id_invoice[]' value='".$row['id_invoice']."' id='r_".$row['id_invoice']."' >
						</td>						
						<td align='left' >
							".$row['number']." 
						</td>
						<td align='left' >
							".$row['name']."
						</td>													
						<td align='left' >
							".format_date($row['due_date'])."
						</td>
						<td align='center' >
							".format($row['amount'],2)."
						</td>	
						<td align='left' >
							<a title='Modificar factura #".$row['id_invoice']."' onClick='$(this).modal({width:350, height:350}).open(); return false;' href='invoices_edit.php?id=".$row['id_invoice']."'>Editar / Comentarios</a>
						</td>																								
						</tr>";
						$total = $total + $row['amount'];
				}//while
				
					$data .="<tr>

						<td align='left' colspan='7'>
							<hr size='1'>
						</td>						
						</tr>";
										
					$data .="<tr>

						<td align='right' colspan='5'>
							Total
						</td>						
						<td align='center' >
							".format($total,2)."
						</td>	
						<td align='left' >
							&nbsp;
						</td>																								
						</tr>";
				
				
			}//if
			else{
				$data .= "<tr><td align='center'>No hay cobros pendientes</td></tr>";			
			}
			$data .="</table>";
		}else{
			error_handling($sql,mysql_error());
		}
		echo $data;
		?>
        &nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>
